﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using Web.UI;
using Model;
using System.Collections;

namespace Web.admin.BasicManage.Academy
{
    public partial class ACA_Add : System.Web.UI.Page
    {
        AcademyService academyService = new AcademyService();
        LocalService localService = new LocalService();
        protected static BoAcademy academyorg;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                localList.DataSource = localService.getAllLocal();
                localList.DataTextField = "LOC_NAME";
                localList.DataValueField = "LOC_ID";
               
                localList.DataBind();

                string operation = Request["OPERATION"];
                if (operation == null)
                    return;
                string academyId = Request["ACA_ID"];
                if (academyId == null)
                    return;

                if (operation == "Modify")
                {
                    ModifyAcademy(academyId);
                }
                else if (operation == "Delete")
                {
                    DeleteAcademy(academyId);
                }
                
            }

        }

        protected void ModifyAcademy(string academyId)
        {
            BoAcademy academy = academyService.getAcademy(academyId);
            academyorg = academy;
            TextBoxName.Text = academy.ACA_NAME;
            ACAIDBOX.Value = academyId;
            localList.SelectedValue = academy.ACA_LOCAL;
            TextBoxCode.Text = academy.ACA_CODE;
            TextBoxEnName.Text = academy.ACA_ENNAME;
            TextBoxRemark.Value = academy.ACA_REMARK;
            TextBoxShort.Text = academy.ACA_SHORT;
            DropDownListIsTeach.SelectedValue = academy.ACA_IsTeach;
        }

        protected void DeleteAcademy(string academyId)
        {
            BoAcademy academy = new BoAcademy();
            academy.ACA_ID = academyId;
            academyService.DeleteAcademy(academy);
            Response.Redirect("ACA_List.aspx");
        }

        protected void ButtonOK_Click(object sender, EventArgs e)
        {
            //if (ACAIDBOX.Value == "")
            //{
            //    BoAcademy academy = new BoAcademy();
            //    academy.ACA_NAME = TextBoxName.Text;
            //    academy.ACA_CODE = TextBoxCode.Text;
            //    academy.ACA_ENNAME = TextBoxEnName.Text;
            //    academy.ACA_IsTeach = DropDownListIsTeach.SelectedValue;
            //    academy.ACA_LOCAL = localList.SelectedValue;
            //    academy.ACA_REMARK = TextBoxRemark.Value;
            //    academy.ACA_SHORT = TextBoxShort.Text;
            //    academyService.CreateAcademy(academy);
            //    Response.Redirect("ACA_List.aspx");
            //}
            //else
            //{
            //    BoAcademy academy = new BoAcademy();
            //    academy.ACA_ID = ACAIDBOX.Value;
            //    academy.ACA_NAME = TextBoxName.Text;
            //    academy.ACA_CODE = TextBoxCode.Text;
            //    academy.ACA_ENNAME = TextBoxEnName.Text;
            //    academy.ACA_IsTeach = DropDownListIsTeach.SelectedValue;
            //    academy.ACA_LOCAL = localList.SelectedValue;
            //    academy.ACA_REMARK = TextBoxRemark.Value;
            //    academy.ACA_SHORT = TextBoxShort.Text;
            //    academyService.UpdateAcademy(academy);
            //    Response.Redirect("ACA_List.aspx");
            ////}
            BoAcademy aca = new BoAcademy();
            aca.ACA_NAME = TextBoxName.Text;
            aca.ACA_CODE = TextBoxCode.Text;
            aca.ACA_ENNAME = TextBoxEnName.Text;
            aca.ACA_IsTeach = DropDownListIsTeach.SelectedValue;
            aca.ACA_LOCAL = localList.SelectedValue;
            aca.ACA_REMARK = TextBoxRemark.Value;
            aca.ACA_SHORT = TextBoxShort.Text;

            BoCheckRepat repeat = new BoCheckRepat();
            repeat.tablename = "Academy";
            repeat.constr = "ACA_IsShow";

            Hashtable keyValue = new Hashtable();
            keyValue.Add("ACA_NAME", aca.ACA_NAME);
            keyValue.Add("ACA_CODE", aca.ACA_CODE);
            keyValue.Add("ACA_LOCAL", aca.ACA_LOCAL);

            repeat.content = keyValue;
            if (string.IsNullOrEmpty(ACAIDBOX.Value))
            {
                if (checkrepeat.checkrepeatCount(repeat) > 0)
                {
                    Response.Write("<script>alert('此对象已经存在，不允许重复')</script>");
                }
                else
                {
                    academyService.CreateAcademy(aca);
                    Response.Redirect("ACA_List.aspx");
                }
            }
            else
            {
                if (academyorg.ACA_NAME == aca.ACA_NAME && academyorg.ACA_CODE == aca.ACA_CODE && academyorg.ACA_LOCAL==aca.ACA_LOCAL )
                {
                    Response.Write("<script>alert('修改成功')</script>");
                    Response.Redirect("ACA_List.aspx");
                }
                else
                {
                    if (checkrepeat.checkrepeatCount(repeat) > 0)
                    {
                        Response.Write("<script>alert('此对象已经存在，不允许重复')</script>");
                    }
                    else
                    {
                        aca.ACA_ID = ACAIDBOX.Value;
                        academyService.UpdateAcademy (aca);
                        Response.Redirect("ACA_List.aspx");
                    }

                }
            }
        }
    }
}